<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="柜号" prop="cabinetCode">
                <el-input
                        v-model="queryParams.cabinetCode"
                        placeholder="请输入柜号"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="门号" prop="doorCode">
                <el-input
                        v-model="queryParams.doorCode"
                        placeholder="请输入门号"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="物品名称" prop="goodsName">
                <el-input
                        v-model="queryParams.goodsName"
                        placeholder="请输入物品名称"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="物品编码" prop="goodsCode">
                <el-input
                        v-model="queryParams.goodsCode"
                        placeholder="请输入物品编码"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="入库数量" prop="inAmount">
                <el-input
                        v-model="queryParams.inAmount"
                        placeholder="请输入入库数量"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="库存数量" prop="repAmount">
                <el-input
                        v-model="queryParams.repAmount"
                        placeholder="请输入库存数量"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="入库时间" style="width: 308px">
                <el-date-picker
                        v-model="daterangeInTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="入库人" prop="inOperator">
                <el-input
                        v-model="queryParams.inOperator"
                        placeholder="请输入入库人"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['goods:goodsIn:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['goods:goodsIn:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['goods:goodsIn:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['goods:goodsIn:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="goodsInList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="入库登记id" align="center" prop="inId" v-if="columns[0].visible"/>
            <el-table-column label="柜号" align="center" prop="cabinetCode" v-if="columns[1].visible"/>
            <el-table-column label="门号" align="center" prop="doorCode" v-if="columns[2].visible"/>
            <el-table-column label="物品名称" align="center" prop="goodsName" v-if="columns[3].visible"/>
            <el-table-column label="物品编码" align="center" prop="goodsCode" v-if="columns[4].visible"/>
            <el-table-column label="入库数量" align="center" prop="inAmount" v-if="columns[5].visible"/>
            <el-table-column label="库存数量" align="center" prop="repAmount" v-if="columns[6].visible"/>
            <el-table-column label="入库时间" align="center" prop="inTime" width="180" v-if="columns[7].visible">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.inTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="入库人" align="center" prop="inOperator" v-if="columns[8].visible"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['goods:goodsIn:edit']">修改</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['goods:goodsIn:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改物品入库对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="goodsInRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="柜号" prop="cabinetCode">
                    <el-input v-model="form.cabinetCode" placeholder="请输入柜号" />
                </el-form-item>
                <el-form-item label="门号" prop="doorCode">
                    <el-input v-model="form.doorCode" placeholder="请输入门号" />
                </el-form-item>
                <el-form-item label="物品名称" prop="goodsName">
                    <el-input v-model="form.goodsName" placeholder="请输入物品名称" />
                </el-form-item>
                <el-form-item label="物品编码" prop="goodsCode">
                    <el-input v-model="form.goodsCode" placeholder="请输入物品编码" />
                </el-form-item>
                <el-form-item label="入库数量" prop="inAmount">
                    <el-input v-model="form.inAmount" placeholder="请输入入库数量" />
                </el-form-item>
                <el-form-item label="库存数量" prop="repAmount">
                    <el-input v-model="form.repAmount" placeholder="请输入库存数量" />
                </el-form-item>
                <el-form-item label="入库时间" prop="inTime">
                    <el-date-picker clearable
                                    v-model="form.inTime"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择入库时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="入库人" prop="inOperator">
                    <el-input v-model="form.inOperator" placeholder="请输入入库人" />
                </el-form-item>
                <el-form-item label="删除标志" prop="delFlag">
                    <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="GoodsIn">
import { listGoodsIn, getGoodsIn, delGoodsIn, addGoodsIn, updateGoodsIn } from "@/api/goods/goodsIn";

const { proxy } = getCurrentInstance();

const goodsInList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeInTime = ref([]);

const columns = ref([
    { key: 0,label: '入库登记id',visible: true } ,
    { key: 1,label: '柜号',visible: true } ,
    { key: 2,label: '门号',visible: true } ,
    { key: 3,label: '物品名称',visible: true } ,
    { key: 4,label: '物品编码',visible: true } ,
    { key: 5,label: '入库数量',visible: true } ,
    { key: 6,label: '库存数量',visible: true } ,
    { key: 7,label: '入库时间',visible: true } ,
    { key: 8,label: '入库人',visible: true }
]);
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        cabinetCode: null,
        doorCode: null,
        goodsName: null,
        goodsCode: null,
        inAmount: null,
        repAmount: null,
        inTime: null,
        inOperator: null,
    },
    rules: {
        cabinetCode: [
            { required: true, message: "柜号不能为空", trigger: "blur" }
        ],
        doorCode: [
            { required: true, message: "门号不能为空", trigger: "blur" }
        ],
        goodsName: [
            { required: true, message: "物品名称不能为空", trigger: "blur" }
        ],
        goodsCode: [
            { required: true, message: "物品编码不能为空", trigger: "blur" }
        ],
        inAmount: [
            { required: true, message: "入库数量不能为空", trigger: "blur" }
        ],
        repAmount: [
            { required: true, message: "库存数量不能为空", trigger: "blur" }
        ],
        inTime: [
            { required: true, message: "入库时间不能为空", trigger: "blur" }
        ],
        inOperator: [
            { required: true, message: "入库人不能为空", trigger: "blur" }
        ],
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询物品入库列表 */
function getList() {
    loading.value = true;
    queryParams.value.params = {};
    if (null != daterangeInTime && '' != daterangeInTime) {
        queryParams.value.params["beginInTime"] = daterangeInTime.value[0];
        queryParams.value.params["endInTime"] = daterangeInTime.value[1];
    }
    listGoodsIn(queryParams.value).then(response => {
        goodsInList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        inId: null,
        cabinetCode: null,
        doorCode: null,
        goodsName: null,
        goodsCode: null,
        inAmount: null,
        repAmount: null,
        inTime: null,
        inOperator: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
    };
    proxy.resetForm("goodsInRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    daterangeInTime.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.inId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加物品入库";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _inId = row.inId || ids.value
    getGoodsIn(_inId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改物品入库";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["goodsInRef"].validate(valid => {
        if (valid) {
            if (form.value.inId != null) {
                updateGoodsIn(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addGoodsIn(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _inIds = row.inId || ids.value;
    proxy.$modal.confirm('是否确认删除物品入库编号为"' + _inIds + '"的数据项？').then(function() {
        return delGoodsIn(_inIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('goods/goodsIn/export', {
        ...queryParams.value
    }, `goodsIn_${new Date().getTime()}.xlsx`)
}

getList();
</script>
